import dbConfig from '../config/db.config.js';
import mysql from 'mysql2/promise';

let pool;

const connectDB = async () => {
  try {
    if (!pool) {
      pool = await mysql.createPool(dbConfig);
      console.log('MySQL Database connected...');
    }
    return pool;
  } catch (error) {
    console.error('Database connection error:', error.message);
    process.exit(1);
  }
};

const getDB = () => {
  if (!pool) {
    throw new Error('Database not connected');
  }
  return pool;
};

export { connectDB, getDB };